package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.api.StockServiceAPI;
import com.dk.mapper.WmItemRecptLineMapper;
import com.dk.mapper.WmItemRecptMapper;
import com.dk.pojo.*;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.RecptService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author LTZ
 * @create 2023/5/11
 */
@Service
public class RecptServiceImpl implements RecptService {
    @Autowired
    private WmItemRecptMapper recptMapper;
    @Autowired
    private WmItemRecptLineMapper recptLineMapper;
    @Autowired
    private StockServiceAPI stockServiceAPI;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageResult<WmItemRecpt> getRecptInfo(Integer pageNum, Integer pageSize, String recptCode, String recptName, String vendorName, String poCode, Date recptDate) {
        WmItemRecptExample example = new WmItemRecptExample();
        if(recptCode != null && recptCode.length() > 0){
            example.createCriteria().andRecptCodeEqualTo(recptCode);
        }
        if(recptName != null && recptName.length() > 0){
            example.createCriteria().andRecptNameLike("%"+recptName+"%");
        }
        if(vendorName != null && vendorName.length() > 0){
            example.createCriteria().andVendorNameLike("%"+vendorName+"%");
        }
        if(poCode != null && poCode.length() > 0){
            example.createCriteria().andPoCodeEqualTo(poCode);
        }
        if(recptDate != null && !"".equals(recptDate)){
            example.createCriteria().andRecptDateEqualTo(recptDate);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<WmItemRecpt> itemRecpts = recptMapper.selectByExample(example);
        PageInfo<WmItemRecpt> pb = new PageInfo<>(itemRecpts);
        PageResult<WmItemRecpt> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;
    }

    @Override
    public Result addRecpt(WmItemRecpt recptRecpt, QcIqc qcIqc, MdVendor mdVendor, Warehouse warehouse, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            recptRecpt.setCreateBy(user.getUsername());
            recptRecpt.setCreateTime(new Date());
            recptRecpt.setStatus("PREPARE");
            recptRecpt.setIqcId(qcIqc.getIqcId());
            recptRecpt.setVendorId(mdVendor.getVendorId());
            recptRecpt.setVendorNick(mdVendor.getVendorNick());
            recptRecpt.setVendorName(mdVendor.getVendorName());
            recptRecpt.setWarehouseId(warehouse.getWid().longValue());
            recptRecpt.setWarehouseCode(warehouse.getCode());
            recptMapper.insertSelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    private void extracted(WmItemRecpt recptRecpt, String token) {
        Claims claims = jwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        User user = JSONObject.parseObject(subject, User.class);
        recptRecpt.setUpdateBy(user.getUsername());
        recptRecpt.setUpdateTime(new Date());
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, QcIqc qcIqc, MdVendor mdVendor, Warehouse warehouse, String token) {
        try {
            extracted(recptRecpt, token);
            recptRecpt.setIqcId(qcIqc.getIqcId());
            recptRecpt.setVendorId(mdVendor.getVendorId());
            recptRecpt.setVendorNick(mdVendor.getVendorNick());
            recptRecpt.setVendorName(mdVendor.getVendorName());
            recptRecpt.setWarehouseId(warehouse.getWid().longValue());
            recptRecpt.setWarehouseCode(warehouse.getCode());
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, MdVendor mdVendor, Warehouse warehouse, String token) {
        try {
            extracted(recptRecpt, token);
            recptRecpt.setVendorId(mdVendor.getVendorId());
            recptRecpt.setVendorNick(mdVendor.getVendorNick());
            recptRecpt.setVendorName(mdVendor.getVendorName());
            recptRecpt.setWarehouseId(warehouse.getWid().longValue());
            recptRecpt.setWarehouseCode(warehouse.getCode());
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, QcIqc qcIqc, Warehouse warehouse, String token) {
        try {
            extracted(recptRecpt, token);
            recptRecpt.setIqcId(qcIqc.getIqcId());
            recptRecpt.setWarehouseId(warehouse.getWid().longValue());
            recptRecpt.setWarehouseCode(warehouse.getCode());
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, QcIqc qcIqc, MdVendor mdVendor, String token) {
        try {
            extracted(recptRecpt, token);
            recptRecpt.setIqcId(qcIqc.getIqcId());
            recptRecpt.setVendorId(mdVendor.getVendorId());
            recptRecpt.setVendorNick(mdVendor.getVendorNick());
            recptRecpt.setVendorName(mdVendor.getVendorName());
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, QcIqc qcIqc, String token) {
        try {
            extracted(recptRecpt, token);
            recptRecpt.setIqcId(qcIqc.getIqcId());
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, MdVendor mdVendor, String token) {
        try {
            extracted(recptRecpt, token);
            recptRecpt.setVendorId(mdVendor.getVendorId());
            recptRecpt.setVendorNick(mdVendor.getVendorNick());
            recptRecpt.setVendorName(mdVendor.getVendorName());
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, Warehouse warehouse, String token) {
        try {
            extracted(recptRecpt, token);
            recptRecpt.setWarehouseId(warehouse.getWid().longValue());
            recptRecpt.setWarehouseCode(warehouse.getCode());
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateRecpt(WmItemRecpt recptRecpt, String token) {
        try {
            extracted(recptRecpt, token);
            recptMapper.updateByPrimaryKeySelective(recptRecpt);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteRecpt(Long recptId) {
        recptMapper.deleteByPrimaryKey(recptId);
    }

    @Override
    public WmItemRecpt selectRecptByRecptId(Long recptId) {
        WmItemRecptExample example = new WmItemRecptExample();
        example.createCriteria().andRecptIdEqualTo(recptId);
        List<WmItemRecpt> wmItemRecpts = recptMapper.selectByExample(example);
        return wmItemRecpts.size() == 0 ? null: wmItemRecpts.get(0);
    }

    @Override
    public Result updateStatus(WmItemRecpt recpt, QcIqc qcIqc) {
        WmItemRecptLineExample lineExample = new WmItemRecptLineExample();
        lineExample.createCriteria().andRecptIdEqualTo(recpt.getRecptId());
        List<WmItemRecptLine> lines = recptLineMapper.selectByExample(lineExample);
        if (lines.size() == 0) {
            return new Result(ResultConstant.LINE_INFO_NULL,ResultConstant.LINE_INFO_NULL_MESSAGE);
        }
        if(qcIqc.getCheckResult().equals("N")){
            return new Result(ResultConstant.CHECK_RESULT_NOT,ResultConstant.CHECK_RESULT_NOT_MESSAGE);
        }
        //如果是未入库状态
        if(recpt.getStatus().equals("PREPARE")){
            recpt.setStatus("SUCCESS");
            WmMaterialStock stock = null;
            for (WmItemRecptLine line : lines) {
                //给库存现有量表增加信息
                stock = new WmMaterialStock();
                //产品物料ID
                stock.setItemId(line.getItemId());
                //产品物料编码
                stock.setItemCode(line.getItemCode());
                //产品物料名称
                stock.setItemName(line.getItemName());
                //规格型号
                stock.setSpecification(line.getSpecification());
                //单位
                stock.setUnitOfMeasure(line.getUnitOfMeasure());
                //入库批次号
                stock.setBatchCode(line.getBatchCode());
                //仓库ID
                stock.setWarehouseId(line.getWarehouseId());
                //仓库编码
                stock.setWarehouseCode(line.getWarehouseCode());
                //仓库名称
                stock.setWarehouseName(line.getWarehouseName());
                //供应商ID
                stock.setVendorId(recpt.getVendorId());
                //供应商名称
                stock.setVendorName(recpt.getVendorName());
                //供应商编号
                stock.setVendorCode(recpt.getVendorCode());
                //供应商简称
                stock.setVendorNick(recpt.getVendorNick());
                //在库数量
                stock.setQuantityOnhand(line.getQuantityRecived());
                //入库时间
                stock.setRecptDate(recpt.getRecptDate());
                //库存有效期
                stock.setExpireDate(new Date());
            }
            stockServiceAPI.addStock(stock);
        }
        recptMapper.updateByPrimaryKeySelective(recpt);
        return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
    }
}